package com.dkd.findmyfriends;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

public class AddPhotosActivity extends Activity implements OnClickListener {

	private static final String TAG = AddPhotosActivity.class.getSimpleName();
	public static final String IMAGES = "IMAGE_IDS";
	
	public static final int GALLERY = 1;
	public static final int CONTACTS = 2;
	public static final int CAMERA = 3;

	private TextView helpText1;
	private TextView helpText2;
	
    @TargetApi(11)
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
            getActionBar().setDisplayHomeAsUpEnabled(true);
		} else {
			requestWindowFeature(Window.FEATURE_NO_TITLE);
		}
		setContentView(R.layout.activity_add_photos);
		
        helpText1 = ((TextView) findViewById(R.id.addPhotosHelper1));
        helpText2 = ((TextView) findViewById(R.id.addPhotosHelper2));
        updateHelpText();
        
        
        PackageManager pm = getPackageManager();
        if(!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA) && !pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)) {
        	findViewById(R.id.cameraBtn).setVisibility(View.INVISIBLE);
        }
    }
    
    private void updateHelpText() {
    	helpText1.setText(getResources().getString(R.string.add_photos_helper_str_1, GameSettings.getMaxNumImages()));
    	helpText2.setText(getResources().getString(R.string.add_photos_helper_str_2, GameSettings.getMaxNumImages()-GameSettings.imagesSelected.size()));
    }

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_add_photos, menu);
        return true;
    }

    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    public void onClick(View v) {
		Intent choose = new Intent(this, MultiImageChooserActivity.class);
		Bundle b = new Bundle();
		b.putInt(MultiImageChooserActivity.MAX_IMAGES_KEY, GameSettings.getMaxNumImages()-GameSettings.imagesSelected.size());
		choose.putExtras(b);
		
		switch (v.getId()) {
			case R.id.galleryBtn:
				choose.putExtra(MultiImageChooserActivity.TYPE_KEY, GALLERY);
				startActivityForResult(choose, GALLERY);			    
				break;
			case R.id.contactBtn:
				choose.putExtra(MultiImageChooserActivity.TYPE_KEY, CONTACTS);
				startActivityForResult(choose, CONTACTS);	
				break;
			case R.id.cameraBtn:
				Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				startActivityForResult(cameraIntent, CAMERA);
				return;
			case R.id.clearBtn:
				GameSettings.imagesSelected.clear();
				updateHelpText();
				break;
			case R.id.startGameBtn:
				Intent play = new Intent(this, PlayGameActivity.class);
				startActivity(play);
		}
	}
    
    @Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		Log.d(TAG, "ResultCode: " + resultCode);
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode != MultiImageChooserActivity.RESULT_CANCELED) {
			switch (requestCode) {
				case GALLERY:
				case CONTACTS:
					Log.d("TEST", "images selected");
					break;
				case CAMERA:
					Cursor cur = getContentResolver().query(
							MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
							new String[]{MediaStore.Images.Media._ID, MediaStore.Images.Media.ORIENTATION},
							null, null,  MediaStore.Images.Media._ID + " DESC LIMIT 1");
					if(cur.moveToNext()) {
						int id =  cur.getInt(cur.getColumnIndex(MediaStore.Images.Media._ID));
						Bitmap thumb = MediaStore.Images.Thumbnails.getThumbnail(
								getContentResolver(), id,
								MediaStore.Images.Thumbnails.MICRO_KIND, null);
							
						Matrix m = new Matrix();
						m.postRotate(cur.getInt(cur.getColumnIndex(MediaStore.Images.Media.ORIENTATION)));
						Bitmap photo = Bitmap.createBitmap(thumb, 0, 0, thumb.getWidth(), thumb.getHeight(), m, true);
						GameSettings.imagesSelected.put(id, photo);
						
					} else {
						Toast.makeText(this, "Camera image not saved.", Toast.LENGTH_LONG).show();
					}
				default:
					break;
				}
			updateHelpText();
		}		
		
		
	}

}
